package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strings"
	"time"
)

type Idiom struct {
	Text string
	Hint string
}

var (
	score   int
	idioms  []Idiom
	chances = 3
)

func initIdioms() {
	idioms = []Idiom{
		{"画蛇添足", "做多余事情的成语"},
		{"刻舟求剑", "拘泥不变的成语"},
		{"望梅止渴", "空想安慰的成语"},
		{"亡羊补牢", "事后补救的成语"},
		{"掩耳盗铃", "自欺欺人的成语"},
	}
}

func selectIdiom() Idiom {
	rand.Seed(time.Now().UnixNano())
	return idioms[rand.Intn(len(idioms))]
}

func gameLoop() {
	reader := bufio.NewReader(os.Stdin)

	for {
		current := selectIdiom()
		fmt.Printf("\n=== 新题目 ===\n提示：%s\n", current.Hint)

		for chances > 0 {
			fmt.Printf("剩余机会 %d ➤ ", chances)
			input, _ := reader.ReadString('\n')
			input = strings.TrimSpace(input)

			if strings.ToLower(input) == "exit" {
				fmt.Println("退出游戏")
				return
			}

			if input == current.Text {
				score += 10
				fmt.Printf("正确！当前得分：%d\n", score)
				break
			} else {
				chances--
				if chances > 0 {
					fmt.Println("错误，请再试")
				}
			}
		}

		if chances == 0 {
			fmt.Printf("游戏结束！正确答案：%s\n", current.Text)
			return
		}
	}
}

func main() {
	initIdioms()
	fmt.Println("=== 成语猜猜乐 ===")
	fmt.Println("规则：")
	fmt.Println("- 根据提示输入完整成语")
	fmt.Println("- 每个成语3次机会")
	fmt.Println("- 输入 exit 退出游戏")
	fmt.Println("-------------------")
	gameLoop()
	fmt.Printf("最终得分：%d\n", score)
}
